#!/usr/bin/env python
# encoding: utf-8
'''
@author: toby
@license: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited.
@contact: hejunwang01@126.com
@software: pycharm
@file: tasks.py
@time: 2021-03-25 23:24
@desc:
'''

# task：任务
# broker（中间人）：存储任务的队列
# worker：真正执行任务的工作者
# backend：用来存储任务执行后的结果

import time
from dbs import db
from db.mydb import MyDB
from excel_com.comm import *
from pytest_pkg.pytest_run import *
from celery_task.celery import cel
import logging

logger = logging.getLogger(__name__)
from models import ApiResult,MyUser,MiniResult,OverView

# 创建celery对象
# 定义任务
@cel.task               #加上此装饰器，这个函数就变成celery任务(task)
def sendMail():
    print('邮件开始发送....')
    for i in range(10):
        print('this is a flask send email-test:{}'.format(i))
        time.sleep(1)
    print('邮件发送结束！')

@cel.task
def apitest():
    print("API接口自动化测试"+"tasks.py task")
    StartRun().runcase()

@cel.task
def mysql_select(sql_str=None):
    # 读取配置文件,链接数据库
    db1 = MyDB('./db/cfg.ini', 'mysql')
    logger.info(db1)

    sql_str = 'SELECT * from t_charge_deal ORDER BY dealId DESC limit 10;'
    if sql_str==None:
        print('参数为空')
        return
    else:
        print(sql_str)
        db1.select_sql(sql_string=sql_str)
        db1.close_()


@cel.task
def newtask():
    print('我是新功能测试....')
    for i in range(10):
        print('this is a newtask:{}'.format(i))
        time.sleep(1)
    print('我是新功能测试  newtask....')


# @cel.task       这里是不能这样执行的 ,只能在view视图中才可以使用sqlach 查询
# def cesutask():
#     print("定时任务,每天统计,然后更新数据库中")
#     try:
#         apiresultcount=ApiResult.query.count()
#         usercount=MyUser.query.count()
#
#         ad_ov = OverView(apitestcount=apiresultcount,
#                          useraddcount=usercount,minitestcount=0,
#                          uitestcount=0,spidertestcount=0,dockertestcount=0)
#         db.session.add(ad_ov)
#         # 提交任务到数据库中
#         db.session.commit()
#
#     except Exception as e:
#         db.session.rollback()





